package com.welsy.iter.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.iter.business.*;
	import com.welsy.iter.vo.*;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.iter.vo.iter.ModuleVO;
	import com.welsy.iter.vo.iter.ElementVO;
	import com.welsy.iter.vo.iter.pattern.IterState;
	import com.welsy.iter.event.PublishIterEvent;
	import com.welsy.iter.vo.iter.pattern.IterState;
	import mx.collections.ArrayCollection;
	import com.welsy.classroom.model.ClassModelLocator;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.welsy.iter.event.ClearExecuteEvent;
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.iter.vo.iter.IterElementVO;
	import com.welsy.user.view.iterview.IterElementView;
	import com.welsy.user.util.StatusManager;	
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class ClearExecuteCommand implements ICommand, IResponder
	{
		private var iterElementId:int;
		
	  	public function ClearExecuteCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var IterEvent:ClearExecuteEvent = ClearExecuteEvent( event );
			
			iterElementId = IterEvent.iterElementID
			
			var delegate:ClearExecuteDelegate = new ClearExecuteDelegate( this );
			delegate.clear(IterEvent.iterElementID,IterEvent.userID);			    			
		}
	
		public function result( event : Object ) : void
		{							
			var resultlist:XMLList = XMLList(event.result)
			var result:String = XML(resultlist.child(0).child(0)).toString()
			
			if (result == "ok") {   		
				changeStatusIterElement();
				Alert.show("Corso ripristinato");				
			}
			else
				Alert.show("Errore nel ripristino del corso")
		}
		
		public function changeStatusIterElement():void
		{
			var model : UserModelLocator = UserModelLocator.getInstance();
			
			var acIterElements:ArrayCollection = ArrayCollection(model.elements)
			var myIterElement:IterElementView
					
			for each (myIterElement in acIterElements) {
				if (int(myIterElement.userCourse.element.iterElementId) == iterElementId) {
					myIterElement.userCourse.status = StatusManager.STATUS_PENDING
					myIterElement.play.enabled = myIterElement.showGoButton()
					myIterElement.replay.enabled = myIterElement.showReplayButton() 
				} 
			}		
		}
		
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show("Iter could not be retrieved!" );
		}
	}

}